<?php

namespace App\Http\Requests\Shop;

use Illuminate\Foundation\Http\FormRequest;

class UserUpdate extends FormRequest
{
   /**
    * Determine if the user is authorized to make this request.
    *
    * @return bool
    */
   public function authorize()
   {
      return true;
   }

   /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */
   public function rules()
   {
      return [
         'name' => 'required|max:255',
         'email' => 'required|email|unique:users,email,' . $this->route('user') . '|max:255',
         'phone' => 'required|regex:/^1[345789][0-9]{9}$/|unique:users,phone,' . $this->route('user') . '|max:11',
         'password' => 'required|min:6|confirmed|max:255',
      ];
   }

   public function messages()
   {
      return [
         'name.required' => '用户名必须填',
         'phone.required' => '手机号必须填',
         'phone.regex' => '手机号格式不正确',
         'email.required' => '邮箱必须填',
         'email.unique' => '该邮箱已注册过',
         'email.email' => '邮箱格式不正确',
         'password.required' => '密码不能为空',
         'password.confirmed' => '确认密码不正确',
      ];
   }

// woo 改变验证后的默认行为
   public function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
   {
      exit(json_encode(array(
         'status' => 0,
         'msg' => $validator->getMessageBag()->first()
      )));
   }
}
